var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

public var gunBullet: Transform;
public var bazukeBullet: Transform;

public var cameras: Camera;

private var nextFire:float = 0.0;
private var rateFire:float = 1.5;

private var gunRate:float = 0.2;
private var miniGunRate:float = 0.1;
public var shootGunRate: float = 0.5;
public var bazukaRate: float = 1.0;

public var mainLife: float = 100.0;

public var miniGunCount: int;
public var shootGunCount: int;
public var bazukaCount: int;

private var ammoType: int = 0;

private var audioOfPlayer: Transform;
private var codeAudioOfPlayer: GamePlayerAudioBehaviour;



function ShotFromGun(){
	audioOfPlayer.audio.Play();
	var clone = Instantiate (gunBullet, transform.position+transform.TransformDirection(Vector3.forward), transform.rotation);
	clone.GetComponent(GameBulletingBehaviour).target = transform;
	nextFire = rateFire;
}

function ShotFromMiniGun(){
	if(miniGunCount>0){
		audioOfPlayer.audio.Play();
		miniGunCount--;
		var clone = Instantiate (gunBullet, transform.position+transform.TransformDirection(Vector3.forward), transform.rotation);
		clone.GetComponent(GameBulletingBehaviour).target = transform;
	}
	nextFire = rateFire;
}

function ShotFromShootGun() {
	if(shootGunCount>0){
		audioOfPlayer.audio.Play();
		shootGunCount--;
		var newLoc: Quaternion = transform.rotation;
		newLoc.eulerAngles.y-=15;
		for(var i:int = 0; i<5; i++){
			newLoc.eulerAngles.y += 5;
			var clone = Instantiate (gunBullet, transform.position+transform.TransformDirection(Vector3.forward), newLoc);
			clone.GetComponent(GameBulletingBehaviour).target = transform;
		}
	}
	nextFire = rateFire;
}

function ShotFromBazuke() {
	if(bazukaCount>0){
		bazukaCount--;
		//bazukeBullet
		var clone = Instantiate (bazukeBullet, transform.position+transform.TransformDirection(Vector3.forward)*2, transform.rotation);
		clone.GetComponent(GameBazukeBehaviour).target = transform;
	}
	nextFire = rateFire;
}

function SetGun(){
	codeAudioOfPlayer.SetAudioClip(0);
	ammoType = 0;
	rateFire = gunRate;	
}

function SetMiniGun(){
	codeAudioOfPlayer.SetAudioClip(0);
	ammoType = 1;
	rateFire = miniGunRate;	
}

function SetShootGun(){
	codeAudioOfPlayer.SetAudioClip(1);
	ammoType = 2;
	rateFire = shootGunRate;	
}

function Start() {
	audioOfPlayer = transform.Find("AudioPlayer");
	codeAudioOfPlayer = audioOfPlayer.GetComponent(GamePlayerAudioBehaviour);
	
	miniGunCount = CoreApplication.miniGunCount;
	shootGunCount = CoreApplication.shootGunCount;
	bazukaCount = CoreApplication.bazukaCount;
	
	SetGun();
	
}

/*function OnDrawGizmos () {
	//Gizmos.color = Color.white;
    //Gizmos.DrawSphere (transform.position, 5.0);  
	 var p = cameras.ScreenToWorldPoint (Vector3 (100,100,cameras.nearClipPlane));
    Gizmos.color = Color.yellow;
    Gizmos.DrawSphere (p, 0.1);
}*/

function Update() {
	/*
	var dx = Input.mousePosition.x - Screen.width / 2;
	var dy = Input.mousePosition.y - Screen.height / 2;
	var strawRadians = Mathf.Atan2(dx,dy);
	var strawDigrees = 360 * strawRadians/(2*Mathf.PI);
	transform.rotation.eulerAngles.y = strawDigrees;
	*/
	
	
	//var mousePos:Vector3 = Input.mousePosition;
	//mousePos.z = -(transform.position.y - Camera.mainCamera.transform.position.y);

	//var worldPos:Vector3 = Camera.mainCamera.ScreenToWorldPoint(mousePos);
	//print(worldPos.x);
	/*
		private var gunRate:float = 0.2;
private var miniGunRate:float = 0.1;
public var shootGunRate: float = 0.5;
public var bazukaRate: float = 1.0;
	
	*/
	//var dx = transform.position.x - worldPos.x;
	//var dy = transform.position.z - worldPos.z;
	//var dy = Input.mousePosition.y - Screen.height / 2;
	if (Input.GetKeyDown (KeyCode.Alpha1)) {
		SetGun();
	}else{
		if(Input.GetKeyDown (KeyCode.Alpha2)){
			SetMiniGun();
		}else{
			if(Input.GetKeyDown (KeyCode.Alpha3)){
				SetShootGun();
			}else{
				if(Input.GetKeyDown (KeyCode.Alpha4)){
					ammoType = 3;
					rateFire = bazukaRate;
				}
			}
		}
	}
	var dx = Input.mousePosition.x - Screen.width / 2;
	var dy = Input.mousePosition.y - Screen.height / 2;
	var strawRadians = Mathf.Atan2(dx,dy);
	var strawDigrees = 360 * strawRadians/(2*Mathf.PI);
	transform.rotation.eulerAngles.y = strawDigrees;
	
	//transform.LookAt(worldPos);
	
	
	
	
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		//moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
/*		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
*/
	}
	if (Input.GetButton ("Fire1") && nextFire<=0) {
		//ShotFromMiniGun();
		//ShowFromBazuke();
		if(ammoType==0){
			ShotFromGun();
		}else{
			if(ammoType==1){
				ShotFromMiniGun();
			}else{
				if(ammoType==2){
					ShotFromShootGun();
				}else{
					ShotFromBazuke();
				}
			}
		}
		//ShotFromShootGan();
		/*miniGunCount--;
		nextFire = rateFire;
	
	
	  /*var p:Vector3 = Camera.mainCamera.ScreenToWorldPoint (Vector3 (100,100,cameras.nearClipPlane));
    Gizmos.color = Color.yellow;
    Gizmos.DrawSphere (p, 0.1);*/
	
	
	
	
		/*audio.Play();
        clone = Instantiate (Bullet1, transform.position+transform.TransformDirection(Vector3.forward), transform.rotation);
		clone.GetComponent(GameBulletingBehaviour).target = transform;*/
    }
	nextFire -= Time.deltaTime;
	

	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)